
//
//  ChangePwViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/3/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit
import Alamofire
import PKHUD
import SwiftyJSON

class ChangePwViewController: BaseViewController, BottomShadow, AddCornerUIRectCorner, UITextFieldDelegate {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var oldTextField: UITextField!
    @IBOutlet weak var newTextField: UITextField!
    @IBOutlet weak var submitButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
    }
    
    func setUI() {
        titleLabel(text: "修改密码", nil)
        addCornerWithView(aView: submitButton, aCorners: [.bottomLeft, .bottomRight], aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        setShadow(view: mainView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
    }
    
    override func setNavigation() {
    }
    
    @IBAction func showPwTap(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
        newTextField.isSecureTextEntry = !sender.isSelected
    }
    
    @IBAction func forgetPwTap(_ sender: UIButton) {
        let retrievePasswordVC = RetrievePasswordViewController(nibName: "\(RetrievePasswordViewController.self)", bundle: nil)
        navigationController?.pushViewController(retrievePasswordVC, animated: true)
        
    }
    
    @IBAction func submitTap(_ sender: UIButton) {
        if !(self.isValidPwd(pwd: oldTextField.text! as NSString)) {
            self.showToast(msg:"请输入8~20位旧密码")
            return
        }
        if !(self.isValidPwd(pwd: newTextField.text! as NSString)) {
            self.showToast(msg:"请输入8~20位新密码")
            return
        }
        
        let requesturl = "\(BaseURL)\(updatePwdUrl)"
        Alamofire.request(requesturl, method: .post,
                          parameters: ["oldpsw":oldTextField.text!,"newpsw":newTextField.text!] ,
                          encoding: URLEncoding.default,
                          headers: ["token":MineInfoModel.share.token!]).responseJSON {
                            (response) in
                            HUD.hide()
                            if(response.error == nil){
                                if let result = response.result.value as? NSDictionary
                                {
                                    let json = JSON(result)
                                    if json["code"].int == 1{
                                        self.showToast(msg:"修改成功")
                                        let logoutrequesturl = "\(BaseURL)\(logoutUrl)"
                                        Alamofire.request(logoutrequesturl, method: .post,
                                                          parameters: nil,
                                                          encoding: URLEncoding.default,
                                                          headers: ["token":MineInfoModel.share.token!]).responseJSON {
                                                            (response) in
                                                            MineInfoModel.share.exitLogin()
                                                            let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
                                                            self.navigationController?.pushViewController(loginVC, animated: true)
                                                            
                                        }
                                    }else if json["code"].int == 100{
                                        self.showToast(msg:json["message"].string!)
                                        let loginVC = LoginViewController(nibName: "\(LoginViewController.self)", bundle: nil)
                                        self.navigationController?.pushViewController(loginVC, animated: true)
                                    }
                                    else{
                                        self.showToast(msg:json["message"].string!)
                                    }
                                }
                            }else{
                                self.showToast(msg:"修改失败")
                            }
        }
        HUD.show(.label("加载中..."))
    }
    @IBAction func editChange(_ sender: Any) {
       
    }
    
    @IBAction func valueChange(_ sender: Any) {
       
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == ""{

            
            return true
        }
        if !(self.isValidPass(text: string)) {
         
            return false
        }
        if !(self.isValidPass(text: string)) {
         
            return false
        }
        
        return true
    }
    
}
